package com.bms.admin.base.company.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.company.entity.Company;
import com.bms.admin.base.company.service.ICompanyService;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 部门管理  前端控制器
 * </p>
 *
 * @author xj
 * @since 2018-08-09
 */
@RestController
@RequestMapping("company")
@Api(value="Company-Api",tags="公司管理")
public class CompanyController extends BaseController {

	@Autowired
	private ICompanyService companyService;
	
	@ApiOperation("获取公司列表")
	@GetMapping("list")
	@RequiresPermissions("company:list")
    public Result list(@RequestParam(defaultValue = "1",required=false)Integer pageNum, @RequestParam(defaultValue = "20",required=false)Integer pageSize) {
		return json(companyService.selectPage(new Page<Company>(pageNum,pageSize)));
    }
	
	@ApiOperation("获取公司信息")
	@GetMapping("info/{companyId}")
	@RequiresPermissions("company:info")
    public Result select(@PathVariable Long companyId){	
		return json(companyService.selectById(companyId));
    }
	
	@Validated
	@ApiOperation("新增公司")
	@PostMapping("save")
	@RequiresPermissions("company:save")
	public Result save(@ApiIgnore @LoginUser NowUser nowUser, @RequestBody Company company){
		Date now = new Date();
		company.setCreateTime(now);
		company.setCreateUser(nowUser.getUsername());
		company.setUpdateTime(now);
		companyService.insert(company);
		return success();
	}
	
	@Validated
	@ApiOperation("修改公司信息")
	@PostMapping("update")
	@RequiresPermissions("company:update")
	public Result update(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody Company company){
		company.setUpdateTime(new Date());
		company.setUpdateUser(nowUser.getUsername());
		companyService.updateById(company);
		return success();
	}
	
	@ApiOperation("删除公司")
	@DeleteMapping("delete")
	@RequiresPermissions("company:delete")
    public Result delete(@RequestBody List<Long> companysId) {
	
		return success();
    }	
	
	
}
